﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPageUser : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["login"] != null)
        {
            User user = Session["login"] as User;
            if (user.UserName != null)
            {
                PanelLogin.Visible = false;
                PanelLogout.Visible = true;
                LabelUsername.Text = "Welcome "+user.UserName;
                HyperLinkPayment.Visible = true;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Page.Validate("login");
        if (Page.IsValid)
        { 
            User user = new UserBll().getUserByUsername(TextBoxUsername.Text);
            if (user.Role == null) Response.Redirect("Error.aspx");
            else
            if (user.Password.CompareTo(TextBoxPassword.Text) == 0)
            {
                if (user.Role.CompareTo("Admin") == 0)
                {
                    Session["login"] = user; 
                    Response.Redirect("Administrator\\HomeAdmin.aspx");                    
                }
                if (user.Role.CompareTo("User") == 0)
                {
                    Session["login"] = user;
                    Response.Redirect("Home.aspx");                    
                }
            }
            else Response.Redirect("Error.aspx");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["login"] = null;
        Session.Clear();
        Session.Abandon();
        PanelLogin.Visible = true;
        PanelLogout.Visible = false;
        HyperLinkPayment.Visible = false;
    }
}
